import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

import {getCaptcha,postRegister,postLogin,getLoginJudge,getExitLogin} from '@/api'


const state = {
    captcha:'',
    userMessage:null
}

const actions = {
    //验证码图片获取
    async reqCaptchaAction(context){
        let result = await getCaptcha();
        if(result.status === 200){
            context.commit('REQCAPTCHAACTION',result.data)
        }
    },

    //发送注册请求
    async reqRegisterAction(context,obj){
        let result = await postRegister(obj);
        return new Promise((res,rej)=>{
            res(result);
        })
    },
    //发送登录请求
    async reqLoginAction(context,obj){
        let result = await postLogin(obj);
        return new Promise((res,rej)=>{
            res(result);
        })
    },
    //判断登录用户
    async reqLoginJudge(context){
        let result = await getLoginJudge();
        context.commit('REQLOGINJUDGE',result.data.data)
        return new Promise((res)=>{
            res(result);
        })
    },
    //退出登录
    async reqExitLoginAction(context){
        let result = await getExitLogin();
        context.commit('REQEXITLOGINACTION');
        return
    }
}

const mutations = {
    REQCAPTCHAACTION(state,value){
        state.captcha = value;
    },
    REQLOGINJUDGE(state,value){
        state.userMessage = value;
    },
    REQEXITLOGINACTION(state){
        state.userMessage = null;
    }
}

const getters = {}

export default {
    namespaced:true,state,mutations,actions,getters
}